package com.yunxi.service.dubboService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.util.WgrDate;
import com.yunxi.model.order.OrderReceipt;
import com.yunxi.order.pojo.OrderReceiptReqVO;
import com.yunxi.order.pojo.OrderReceiptVO;
import com.yunxi.hubToBiz.pojo.PushOrderReceiptReqVO;
import com.yunxi.hubToBiz.pojo.PushOrderReceiptRespVO;
import com.yunxi.order.service.OrderReceiptDubboService;
import com.yunxi.service.localService.OrderReceiptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 发票表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-29
 */
public class OrderReceiptDubboServiceImpl implements OrderReceiptDubboService {

    private final Logger logger = LoggerFactory.getLogger(OrderReceiptDubboServiceImpl.class);

    @Autowired
    @Qualifier(value = "orderReceiptServiceImpl")
    OrderReceiptService orderReceiptService;


    @Override
    @Transactional
    @LcnTransaction
    public OrderReceiptVO getById(String id) throws InvocationTargetException, IllegalAccessException {
    OrderReceipt orderReceipt = orderReceiptService.getById(id);
    OrderReceiptVO orderReceiptVO = new OrderReceiptVO();
        BeanUtils.copyProperties(orderReceipt,orderReceiptVO);
        return orderReceiptVO;
    }

    @Override
    @Transactional
    @LcnTransaction
    public void saveOrUpdate(OrderReceiptVO orderReceiptVO) throws InvocationTargetException, IllegalAccessException {
    OrderReceipt orderReceipt = new OrderReceipt();
        BeanUtils.copyProperties(orderReceiptVO,orderReceipt);
    orderReceiptService.saveOrUpdate(orderReceipt);
    }

    @Override
    @Transactional
    @LcnTransaction
    public void removeById(String id) {
    orderReceiptService.removeById(id);
    }

    @Override
    @Transactional
    @LcnTransaction
    public PageInfo<OrderReceiptVO> findPageByVO(OrderReceiptVO orderReceiptVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
      PageInfo<OrderReceiptVO> pageInfo = orderReceiptService.pageByVo(orderReceiptVO,pageNum,pageSize);
      return pageInfo;
   }

    @Override
    @Transactional
    @LcnTransaction
    public void removeByListVo(List<OrderReceiptVO> orderReceiptVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        orderReceiptService.removeByListVo(orderReceiptVOList);
    }

    @Override
    @Transactional
    @LcnTransaction
    public List listByVo(OrderReceiptVO orderReceiptVO) {
        return orderReceiptService.listByVo(orderReceiptVO);
    }

    @Override
    @Transactional
    @LcnTransaction
    public boolean signTo(OrderReceiptReqVO vo) {

        if(vo == null || vo.getReceiptIds() == null){
            throw new RuntimeException("VO对象为Null,签收失败！");
        }

        for(int i = 0; i < vo.getReceiptIds().size(); i ++){
            QueryWrapper qw = new QueryWrapper();
            qw.eq("RECEIPT_ID", vo.getReceiptIds().get(i));

            OrderReceipt orderReceipt = new OrderReceipt();
            orderReceipt.setIsSign(vo.getIsSign());
            orderReceipt.setSignDate(new Date());
            boolean isSuccess = orderReceiptService.update(orderReceipt,qw);

            if(!isSuccess){
                logger.info("修改保存批售单失败，事务回滚！");
                throw new RuntimeException("修改保存批售单失败，事务回滚！");
            }
        }
        return true;
    }
}
